using System;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using XpathTool.Helper;
using System.Collections.ObjectModel;
using HtmlAgilityPack;
using XpathTool.Model;
using System.Collections.Generic;

namespace XpathTool.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {

        private string _HtmlText;
        public string HtmlText
        {
            get { return _HtmlText; }
            set
            {
                _HtmlText = value;
                RaisePropertyChanged();
            }
        }

        private string _UrlText;
        public string UrlText
        {
            get { return _UrlText; }
            set
            {
                _UrlText = value;
                RaisePropertyChanged();
            }
        }

        private int _UrlType;
        public int UrlType
        {
            get { return _UrlType; }
            set
            {
                _UrlType = value;
                RaisePropertyChanged();
            }
        }
        private XpathObj _SelectedItem;
        public XpathObj SelectedItem
        {
            get { return _SelectedItem; }
            set
            {
                _SelectedItem = value;
                RaisePropertyChanged();
                SetSelectedString(_SelectedItem);
            }
        }

        private string _SelectedString;
        public string SelectedString
        {
            get { return _SelectedString; }
            set
            {
                _SelectedString = value;
                RaisePropertyChanged();
            }
        }

        HtmlDocument hd = new HtmlDocument();
        string loghtml = "";
        private void SetSelectedString(XpathObj obj)
        {
            if (hd == null) hd.LoadHtml(HtmlText);
            try
            {
                SelectedString = hd.DocumentNode.SelectSingleNode(obj.XPath).InnerHtml;
            }
            catch
            { SelectedString = "Error"; }

        }


        private ObservableCollection<XpathItem> _LVList;
        public ObservableCollection<XpathItem> LVList
        {
            get { return _LVList; }
            set
            {
                _LVList = value;
                RaisePropertyChanged();
            }
        }


        private ObservableCollection<XpathObj> _HNList;
        public ObservableCollection<XpathObj> HNList
        {
            get { return _HNList; }
            set
            {
                _HNList = value;
                RaisePropertyChanged();
            }
        }
        private RelayCommand _AnalysisCommand = null;
        public RelayCommand AnalysisCommand
        {
            get
            {
                if (_AnalysisCommand == null)
                    _AnalysisCommand = new RelayCommand(() => Analysis());
                return _AnalysisCommand;
            }
        }



        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            UrlType = 0;

        }

        private void Analysis()
        {
            XPathHelper.Instance.GetHtmlNode(HtmlText);
            HNList = new ObservableCollection<XpathObj>(XPathHelper.Instance.list);
            LVList = new ObservableCollection<XpathItem>();
            LVList.Add(XPathItemHelper.Instance.GetHtmlNode(HtmlText));
            hd.LoadHtml(HtmlText);

        }
    }
}